package com.zdy.im.user.controller;

import com.sixeco.framework.common.api.ApiDoc;
import com.sixeco.framework.common.enums.SysResultCodeEnum;
import com.sixeco.framework.http.handler.OutputResponse;
import com.zdy.im.enums.ImResultCodeEnum;
import com.zdy.im.user.ImErrorMsg;
import com.zdy.im.user.dto.response.UserInfoResponseDto;
import com.zdy.im.user.facade.InitFacade;
import com.zdy.im.user.service.UserInfoService;
import com.zdy.im.user.vo.request.ImLoginRequestVo;
import com.zdy.im.user.vo.response.Oauth2TokenDto;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.provider.endpoint.TokenEndpoint;
import org.springframework.stereotype.Controller;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.security.Principal;
import java.util.Map;
@ApiDoc(
        module = "获取token模块", isApp = true
)
@Controller
public class AuthController {

    @Autowired
    private TokenEndpoint tokenEndpoint;
    @Autowired
    private UserInfoService userInfoService;
    @Autowired
    private InitFacade initFacade;

    /**
     * Oauth2登录认证
     */
    @ApiDoc(
            value = "登录返回token",
            request = ImLoginRequestVo.class,
            response = Oauth2TokenDto.class
    )
    @RequestMapping(value = "/oauth/token", method = RequestMethod.POST)
    public void postAccessToken(OutputResponse out, Principal principal, @RequestParam Map<String, String> parameters) throws HttpRequestMethodNotSupportedException {
        parameters.put("client_id", "v-client");
        parameters.put("client_secret", "v-client-ppp");
        parameters.put("grant_type", "password");
        parameters.put("scope", "select");
        if(parameters.containsKey("type") && 1 == Integer.valueOf(parameters.get("type")).intValue()){
            out.write( initFacade.customerInit(principal));
            return;
        }
        String loginName = parameters.get("username");
        if(StringUtils.isEmpty(loginName)){
            out.write(SysResultCodeEnum.SYS_PARAM_ERROR.formatMsg(ImErrorMsg.User.USER_LOGIN_NAME_NULL));
            return;
        }
        UserInfoResponseDto user = userInfoService.getUserInfoByLoginName(loginName);
        if(user == null){
            out.write(ImResultCodeEnum.IM_USER_NOT_EXIST);
            return;
        }
        OAuth2AccessToken oAuth2AccessToken = tokenEndpoint.postAccessToken(principal, parameters).getBody();
        Oauth2TokenDto oauth2TokenDto = Oauth2TokenDto.build()
                .toAccess_token(oAuth2AccessToken.getValue())
                .toRefresh_token(oAuth2AccessToken.getRefreshToken().getValue())
                .toExpires_in(oAuth2AccessToken.getExpiresIn())
                .toToken_type(oAuth2AccessToken.getTokenType())
                .toUser_id(user.getId());

        out.write(oauth2TokenDto);
    }
}
